package com.wjm.myalipayplugin;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.wjm.pluginstand.PayInterfaceActivity;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ProxyActivity extends Activity {

    private PayInterfaceActivity payInterfaceActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String className = getIntent().getStringExtra("className");
        try {
            Class<?> aClass = getClassLoader().loadClass(className);
            Constructor<?> constructor = aClass.getConstructor(new Class[]{});
            Object instance = constructor.newInstance(new Object[]{});
            payInterfaceActivity = (PayInterfaceActivity) instance;//将加载到的插件apk中的相应activity类强制转换为插件开发规范接口对象，因为插件中的activity类都是面对此接口开发的，所可以
            payInterfaceActivity.attach(this);//并向插件activity中传递宿主上下文
            Bundle bundle = new Bundle();//如果要封装参数放在此对象中
            payInterfaceActivity.onCreate(bundle);//插件中的activity通过宿主来启动，调用插件activity相应生命同期方法，管理插件activity的生命同期
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    //重写此方法，那么插件中想启动activity的跳转实际是还是用的代理来驱动，因为插件apk中任何的actvitiy又不用安装又没有注册系统中是找不到的，只有通过代理类驱动它，让它的生命周期附附托在此代理activity类上
    @Override
    public void startActivity(Intent intent) {
        String className1 = intent.getStringExtra("className");
        Intent intent1 = new Intent(this, ProxyActivity.class);
        intent1.putExtra("className", className1);
        super.startActivity(intent1);
    }

    //插件中启动服务与启动activity类似，要附托在一个宿主apk中的代理服务类之上
    @Override
    public ComponentName startService(Intent service) {
        String serviceName = service.getStringExtra("serviceName");
        Intent intent1 = new Intent(this, ProxyService.class);
        intent1.putExtra("serviceName", serviceName);
        return super.startService(intent1);
    }

    //插件中的通过此代理类管理，它的类加载器实际是宿主在PluginManager中通过插件apk文件去加载到的插件的类加载器的
    @Override
    public ClassLoader getClassLoader() {
        return PluginManager.getInstance().getDexClassLoader();
    }

    //插件中的通过此代理类管理，它的资源对象Resources实际是宿主在PluginManager中通过插件apk文件去加载到的插件的Resources
    @Override
    public Resources getResources() {
        return PluginManager.getInstance().getResources();
    }


    //同样，插件的activity的生命周期通过代理类来驱动
    @Override
    protected void onStart() {
        super.onStart();
        payInterfaceActivity.onStart();
    }

    //同样，插件的activity的生命周期通过代理类来驱动
    @Override
    protected void onDestroy() {
        super.onDestroy();
        payInterfaceActivity.onDestroy();
    }

    //同样，插件的activity的生命周期通过代理类来驱动
    @Override
    protected void onPause() {
        super.onPause();
        payInterfaceActivity.onPause();
    }

    @Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        IntentFilter intentFilter=new IntentFilter();
        for(int i=0;i<filter.countActions();i++){
            intentFilter.addAction(filter.getAction(i));
        }
        Log.d("TAG","宿主中==>registerReceiver==》宿主app中的代理activity中注册广播==>"+receiver.getClass().getName());
        return super.registerReceiver(new ProxyBroadCast(receiver.getClass().getName(),this), intentFilter);
//        return super.registerReceiver(receiver, filter);
    }
}
